package com.cloudxingkong.dianyinke.NewTeacher.Utils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import static java.lang.System.currentTimeMillis;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.net.URLConnection.guessContentTypeFromName;
import static java.text.MessageFormat.format;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.UUID;
import java.util.logging.Logger;


/**
 * Created by LeeQ on 2016-4-19.
 */
public class UploadUtils {

    public static void postData(RequestParams params, final UploadCallback callback) {
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callback.onFinish(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public static String getData(String requestURL, String sessionID, UploadCallback callback){
        HttpURLConnection conn=null;
        try {
            URL url=new URL(requestURL);
            conn= (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Cookie", sessionID);
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(10000);

            int code=conn.getResponseCode();
            if (code == 200) {
                InputStream is = conn.getInputStream();
                String state = getStringFromInputStream(is);
                callback.onFinish(state);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return requestURL;
    }
    private static String getStringFromInputStream(InputStream is) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buf=new byte[1024*10];
        int len=-1;
        while ((len=is.read(buf))!=-1){
            os.write(buf,0,len);
        }
        is.close();
        String state = os.toString();
        os.close();
        return state;
    }



    public interface UploadCallback{
        public void onFinish(String state);

    }
}
